//
//  QianDViewController.m
//  Estate
//
//  Created by heyong on 16/5/30.
//  Copyright © 2016年 ttsx. All rights reserved.
//
#import <CoreLocation/CLLocationManager.h>

#import "QianDViewController.h"
#import "QianDTableCell.h"
#import "DefaineCell.h"
#import "QianDImageCell.h"
#import <MJExtension.h>


#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件


@interface QianDViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    NSArray *titArr;
    UITextField *_curentTF;
}

//记录地点
@property (copy, nonatomic)NSString *goodstitle;
//经纬度
@property (nonatomic,copy) NSString *latitude;
@property (nonatomic,copy) NSString *longitude;
@property (nonatomic,copy) NSString *baiduAddress;


//地区
@property (copy, nonatomic)NSString *address;
//天气
@property (copy, nonatomic)NSString *weather;

@property(strong, nonatomic)NSMutableArray *textArr;

@property (nonatomic, strong)BMKLocationService *locService;
@property (nonatomic, strong)BMKGeoCodeSearch *geocodesearch;
@property BOOL isGeoSearch;
@property (nonatomic,assign) BOOL isDingwei;

@end

@implementation QianDViewController
- (NSMutableArray *)weatherArr{
    if(_weatherArr==nil){
        _weatherArr=[NSMutableArray array];
    }
    return _weatherArr;
    
}
-(NSMutableArray *)textArr{
    if(!_textArr){
        _textArr=[NSMutableArray array];
    }
    return _textArr;
}

- (UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.bounces=NO;
        [_tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"qiandao"];
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"defaine"];
        [_tableView registerNib:[UINib nibWithNibName:@"QianDImageCell" bundle:nil] forCellReuseIdentifier:@"QianDImageCell"];
        
        _tableView.layer.borderColor=[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00].CGColor;
        _tableView.layer.borderWidth=1;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
        //定位功能可用
        self.isDingwei = YES;
        
    }else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
        self.isDingwei = NO;
        //定位不能用
    }
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    self.title=@"签到";
    [self createView];
    
    if(self.dict!=nil){
        self.title=@"记录查看";
    }
    //天气数组
    NSMutableArray *wArr=DEF_PERSISTENT_GET_OBJECT(@"WEATHER");
    if(wArr.count>0){
        for (NSDictionary *wdic in wArr) {
            for (NSString *city in wdic) {
                [self.weatherArr addObject:city];
            }
        }
        NSDictionary *dic=[wArr firstObject];
        NSString *deCity=[self.weatherArr firstObject];
        self.address=deCity;
        self.weather=dic[self.address];
    }
    
}

//!!!!:签到
- (void)clickSave{
    if([self.signIntime isEqualToString:@"时间获取异常"]){
        [LSUtility showToastWithMessage:@"时间获取异常请检查网络情况!" inView:self.view Andtime:2];
        return;
    }
    if([self.signIntime isEqualToString:@"时间获取中"]){
        [LSUtility showToastWithMessage:@"时间获取异常请检查网络情况!" inView:self.view Andtime:2];
        return;
    }
    
    if([self.goodstitle isEqualToString:@""]||self.goodstitle==NULL){
        [LSUtility showToastWithMessage:@"请输入地址" inView:self.view Andtime:1.5];
        return;
    }
    
    [self operationDB];
    
    [self.navigationController popViewControllerAnimated:YES];
}

//数据库操作
- (void)operationDB{
    //图片存入文件
    NSString *imageName=[LSUtility getCurrentDateString];
    
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    //插入数据
    DBHelper *dbhelper=[DBHelper help];
    
    /*
     * 附件表插入数据
     */
    NSString *state;
    if([self.recordType isEqualToString:@"RECORD_06"]){
        state=@"RECORD_05";
    }else{
        state=@"RECORD_06";
    }
    
    NSString *fileName=[LSUtility getCurrentDateString];
    //附件表id
    NSString *ids=[NSUUID UUID].UUIDString;
    //记录表id
    NSString *fileId=[NSUUID UUID].UUIDString;
    
    //存入附件表
    [LSUtility insertAttachmentFromFileID:fileId andTableID:ids andImage:self.image andType:state andImageName:imageName];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:ids forKey:@"fileId"];
    [dict setObject:@"1" forKey:@"fileState"];
    
    NSArray *arrDic=@[dict];
    
    /*
     *  记录表里插入数据
     */
    //设置title
    NSString *title;
    //签到
    if([state isEqualToString:@"RECORD_05"]){
        title=[NSString stringWithFormat:@"%@在[%@]签到",
               DEF_PERSISTENT_GET_OBJECT(@"realname"),
               self.goodstitle];
    }
    //签退
    if([state isEqualToString:@"RECORD_06"]){
        title=[NSString stringWithFormat:@"%@在[%@]签退",
               DEF_PERSISTENT_GET_OBJECT(@"realname"),
               self.goodstitle];
    }
    
    NSString *insertWether=[NSString stringWithFormat:@"%@/%@",self.address,self.weather];
    
    NSString *stateRole=@"";
    if([USERROLE isEqualToString:@"USER_01"]){
        stateRole=@"0";
    }else if([USERROLE isEqualToString:@"USER_03"]||[USERROLE isEqualToString:@"USER_07"]){
        stateRole=@"1";
    }else if([USERROLE isEqualToString:@"CONSTRACT_01"]||[USERROLE isEqualToString:@"CONSTRACT_02"]||[USERROLE isEqualToString:@"CONSTRACT_03"]||[USERROLE isEqualToString:@"CONSTRACT_04"]||[USERROLE isEqualToString:@"CONSTRACT_05"]||[USERROLE isEqualToString:@"CONSTRACT_06"]||[USERROLE isEqualToString:@"CONSTRACT_07"]){
        stateRole=@"2";
    }else{
        stateRole=@"";
    }
    
    NSDictionary *addinfo = [NSMutableDictionary dictionary];
    [addinfo setValue:self.goodstitle forKey:@"address"];
    [addinfo setValue:self.baiduAddress forKey:@"upDistanceStr"];
    [addinfo setValue:DEFISNUL(self.latitude) forKey:@"gps_lat"];
    [addinfo setValue:DEFISNUL(self.longitude) forKey:@"gps_long"];
    NSString *addinfostr =[LSUtility dictionaryToJson:addinfo];
    NSDictionary *keyValue=@{@"ID":fileId,
                             @"PRO_ID":PROID,
                             @"RECORD_USER":USERID,
                             @"RECORD_MASTER":@"",
                             @"RECORD_SUPERVISION_USER":@"",
                             @"ADDRESS_ID":@"",
                             @"RECORDONTENT":@"",
                             @"RECORD_INFO":@"",
                             @"RECORD_NUMBER":@"",
                             @"EXTEND_INFO":@"",
                             @"MODIFY_USER_NAME":@"",
                             @"MODIFY_IP":@"",
                             @"MODIFY_TIME":@"",
                             @"LOCAL_MODIFY_USER_NAME":@"",
                             @"LOCAL_MODIFY_TIME":@"",
                             @"WEATHER":insertWether,
                             @"ADDRES_INFO":addinfostr,
                             @"RECORD_TITLE":title,
                             @"RECORD_TYPE":state,
                             @"RECORD_FILE_PATH":[LSUtility dictionaryToJson_2:arrDic],
                             @"STATE":stateRole,
                             @"CREATE_TIME":self.signIntime,
                             @"CREATE_IP":[LSUtility getIPAddress],
                             @"LOCAL_MODIFY_STATE":@"ADD",
                             @"CREATE_USER_NAME":USERID};
    BOOL isse=[dbhelper DataBase:db insertKeyValues:keyValue intoTable:WORK_RECORD];
    /*
     *  照片保存至本地
     *  名称存储为filePath的id
     */
    //存入清理文件夹
    [LSUtility saveImage: self.image withName:fileName];
    //存入附件永久文件夹
    [LSUtility saveImagePermanent:self.image withName:imageName];
    
    if (isse) {
        DeLog(@"签到成功");
        NSNotification * notice = [NSNotification notificationWithName:@"signIn" object:nil userInfo:nil];
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        //   indexView
        
    }else{
        [MBProgressHUD showError:@"签到失败！" toView:self.view];
        DeLog(@"签到失败");
    }
}

//!!!!:创建页面
- (void)createView{
    
    if(_dict){//存在信息，显示签到查看页
        self.address=_dict[@"address"];
        NSDictionary *addInfo = [LSUtility dictionaryWithJsonString:_dict[@"addInfo"]];
        if (addInfo) {
            self.goodstitle = addInfo[@"address"];
            self.baiduAddress = addInfo[@"upDistanceStr"];
        }else{
            self.goodstitle=_dict[@"addInfo"];
            self.baiduAddress = @"";
        }
        self.weather=_dict[@"weather"];
        [self.textArr addObject:_dict[@"name"]];
        [self.textArr addObject:[_dict[@"type"] isEqualToString:@"RECORD_05"]?@"签到":@"签退"];
        [self.textArr addObject:_dict[@"time"]];
        [self.textArr addObject:_dict[@"address"]];
        [self.textArr addObject:_dict[@"weather"]];
        [self.textArr addObject:_dict[@"addInfo"]];
        [self.textArr addObject:@""];
    }else{
        
        WEAKSELF
        //是否必须使用服务器时间
        if(self.isServer){
            
            //判断是否存在网络
            [LSHttpTool checkNetWork:^(NSString *status) {
                //有网
                if(![status isEqualToString:@"0"]){
                    //获得服务器时间
                    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
                    [LSHttpTool Post:URL_SERVERTIME parameters:dic success:^(id responseObject) {
                        NSString *code=responseObject[@"code"];
                        if([code isEqualToString:@"1"]){
                            NSString *time=responseObject[@"data"];
                            weakSelf.signIntime=time;
                        }else{
                            weakSelf.signIntime= @"时间获取异常";
                        }
                        [self loadData];
                    } failure:^(NSError *error) {
                        weakSelf.signIntime= @"时间获取异常";
                        [self loadData];
                        
                    }];
                }
                //无网
                else{
                    weakSelf.signIntime= @"时间获取异常";
                    [self loadData];
                }
            }];
        }else{
            //判断是否存在网络
            [LSHttpTool checkNetWork:^(NSString *status) {
                //有网
                if(![status isEqualToString:@"0"]){
                    //获得服务器时间
                    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
                    [LSHttpTool Post:URL_SERVERTIME parameters:dic success:^(id responseObject) {
                        NSString *code=responseObject[@"code"];
                        if([code isEqualToString:@"1"]){
                            NSString *time=responseObject[@"data"];
                            weakSelf.signIntime=time;
                        }else{
                            weakSelf.signIntime= @"时间获取异常";
                        }
                        [self loadData];
                        
                    } failure:^(NSError *error) {
                        weakSelf.signIntime= @"时间获取异常";
                        [self loadData];
                    }];
                }
                //无网
                else{
                    //获得手机时间
                    weakSelf.signIntime= [LSUtility TimeNow];
                    [self loadData];
                }
            }];
        }
        
    }
    
    [self createViews];
    
    
}

-(void)loadData{
    //设置编辑签到 签退内容//判断状态
    [self.textArr  removeAllObjects];
    if([self.recordType isEqualToString:@"RECORD_06"]){//签到
        [self.textArr addObject:DEF_PERSISTENT_GET_OBJECT(@"realname")];
        [self.textArr addObject:@"签到"];
        [self.textArr addObject:self.signIntime];
        [self.textArr addObject:@"地区"];
        [self.textArr addObject:@"天气"];
        [self.textArr addObject:@""];
        [self.textArr addObject:@""];
        
        self.title = @"签到";
    }else{//签退
        [self.textArr addObject:DEF_PERSISTENT_GET_OBJECT(@"realname")];
        [self.textArr addObject:@"签退"];
        [self.textArr addObject:self.signIntime];
        [self.textArr addObject:@"地区"];
        [self.textArr addObject:@"天气"];
        [self.textArr addObject:@""];
        [self.textArr addObject:@""];
        self.title = @"签退";
        
        
    }
    if(self.isDingwei){
        [self startUserLocationService];
    }
    [self.tableView reloadData];
    //添加保存视图
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
    
}


-(void)createViews{
    self.view.backgroundColor= [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    //表格
    [self.view addSubview:self.tableView];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    titArr=[[NSArray alloc]initWithObjects:@"记   录  人  :",
            @"记 录 状 态 :",@"记 录 时 间 :",@"地\t \t区  :",@"天\t \t气  :",@"记 录 地 点 :", @"您定位的位置",nil];
}



- (void)imageTap:(UITapGestureRecognizer *)tap{
    UIImageView *img=(UIImageView *)tap.view;
    if(img){
        [LSTarBrowser showImage:(UIImageView *)tap.view];
    }
}


#pragma mark-UITableViewDelagate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.mj_w, 50)];
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 40)];
    lab.font=[UIFont systemFontOfSize:14.];
    lab.textColor=[UIColor colorWithRed:0.38 green:0.67 blue:0.66 alpha:1.00];
    [view addSubview:lab];
    if (section == 0) {
        lab.text = @"记 录 信 息";
    }else{
        lab.text=@"附 件 信 息";
    }
    return view;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section ==0){
        return self.textArr.count;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }else{
        return 200;
    }
}
#pragma mark-UITableViewDataSourse
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf=self;
    long row=indexPath.row;
    if (indexPath.section == 0) {
        if(row ==0 ||row ==1||row==2){
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellStyleDefault;
            [cell setTitle:titArr[row] andDetail:self.textArr[row] andLeftConstraint:8];
            return cell;
            
        }else if (row==3){
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellStyleDefault;
            [cell setTitle:titArr[row] andDetail:DEFISNUL(self.address) andLeftConstraint:8];
            return cell;
        }else if (row==4){
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellStyleDefault;
            [cell setTitle:titArr[row] andDetail:DEFISNUL(self.weather) andLeftConstraint:8];
            return cell;
            
        }else if(row ==5 ){
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qiandao" forIndexPath:indexPath];
            cell.textField.tag=indexPath.row;
            cell.textField.delegate=self;
            if(_dict){
                cell.isedit=NO;
            }
            
            if(self.isDingwei){
                if (self.goodstitle.length==0 && self.baiduAddress.length>0) {
                    if(![self.baiduAddress isEqualToString:@"获取地理信息位置失败"]){
                        self.goodstitle = self.baiduAddress;
                    }
                }
            }
            cell.textValueChangedBlock = ^(NSString *valueStr){
                weakSelf.goodstitle = valueStr;
            };
            [cell configWithPlaceholder:@"请输入地点" andValue:DEFISNUL(weakSelf.goodstitle) andLabel:@"记 录 地 点 :" andKeyType:UIKeyboardTypeDefault];
            return cell;
        }else{
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qiandao" forIndexPath:indexPath];
            cell.isedit=NO;
            NSString *value;
            if(self.isDingwei){
                value = @"正在获取当前所在地";
                if (self.baiduAddress.length>0) {
                    value = self.baiduAddress;
                }
            }else
            {
                value = @"请开启定位权限后重试";
            }
            
            [cell configWithPlaceholder:@"" andValue:DEFISNUL(value) andLabel:@"您定位的位置:" andKeyType:UIKeyboardTypeDefault];
            return cell;
        }
        
    }else{
        QianDImageCell  *cell=[tableView dequeueReusableCellWithIdentifier:@"QianDImageCell"];
        
        if(_dict){
            NSString *fileID=_dict[@"fileId"];
            NSString *fileName=[LSUtility queryAttachment:fileID][@"FILE_NAME"];
            cell.qianDImage.image =[LSUtility getImageByFileName:fileName];
        }else{
            cell.qianDImage.image=self.image;
        }
        //image添加点击事件
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTap:)];
        [cell.qianDImage addGestureRecognizer:tap];
        return cell;
    }
}
-(void)clickBtn{
    [self.tableView reloadData];
}
#pragma  mark--点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    if(!_dict){
        if(indexPath.row==3){
            [self.view endEditing:YES];
            NSNumber *ninit = [NSNumber numberWithInteger:0];
            NSMutableArray *wArr=DEF_PERSISTENT_GET_OBJECT(@"WEATHER");
            [ActionSheetStringPicker showPickerWithTitle:nil rows:@[self.weatherArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                
                NSDictionary *weadic=wArr[iv];
                NSString *address =self.weatherArr[iv];
                
                self.address=address;
                self.weather=weadic[address];
                
                //刷新城市、刷新天气
                NSIndexPath *windexPath=[NSIndexPath indexPathForRow:4 inSection:0];
                
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath,windexPath] withRowAnimation:UITableViewRowAnimationNone];
                
            } cancelBlock:nil origin:self.view];
            
        }
        
    }
}

/*
 *点击屏幕结束编辑
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    _curentTF=textField;
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //   限制苹果系统输入法  禁止输入表情
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}

- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardFrame.origin.y;
    CGFloat textField_maxY = (_curentTF.tag + 1) * 50;
    CGFloat space =  150*BIZ + textField_maxY;
    CGFloat transformY = height - space;
    if (transformY < 0) {
        CGRect frame = self.view.frame;
        frame.origin.y = transformY ;
        self.view.frame = frame;
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    //恢复到默认y为0的状态，有时候要考虑导航栏要+64
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    self.view.frame = frame;
}
-(void)startUserLocationService{
    [self.locService startUserLocationService];//启动定位服务
}
#pragma mark - BMK_LocationDelegate 百度地图
/**
 *定位失败后，会调用此函数
 *@param error 错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"地图定位失败======%@",error);
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    
    //存储经纬度
    self.latitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude];
    self.longitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];;
    if (self.latitude.length>0 || self.longitude.length >0)
    {
        //发送反编码请求
        [self reverseGeoCodeWithLatitude:self.latitude withLongitude:self.longitude];
    }else{
        NSLog(@"位置为空");
    }
    
    //关闭坐标更新
    [self.locService stopUserLocationService];
}

//地图定位
- (BMKLocationService *)locService
{
    if (!_locService)
    {
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
    }
    return _locService;
}

//检索对象
- (BMKGeoCodeSearch *)geocodesearch
{
    if (!_geocodesearch)
    {
        _geocodesearch = [[BMKGeoCodeSearch alloc] init];
        _geocodesearch.delegate = self;
    }
    return _geocodesearch;
}

#pragma mark ----反向地理编码
- (void)reverseGeoCodeWithLatitude:(NSString *)latitude withLongitude:(NSString *)longitude
{
    
    CLLocationCoordinate2D coor;
    coor.latitude = [latitude doubleValue];
    coor.longitude = [longitude doubleValue];
    
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
    reverseGeocodeSearchOption.reverseGeoPoint = coor;
    BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if (flag)
    {
        NSLog(@"反地理编码成功");//可注释
    }
    else
    {
        NSLog(@"反地理编码失败");//可注释
        self.baiduAddress = @"获取地理信息位置失败";
        [self.tableView reloadData];
    }
}



-(void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    
    NSLog(@"%@",result);
}
//发送成功,百度将会返回东西给你
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher
                          result:(BMKReverseGeoCodeResult *)result
                       errorCode:(BMKSearchErrorCode)error
{
    
    if (error == BMK_SEARCH_NO_ERROR) {
        if (result.poiList.count>0) {
            BMKPoiInfo *info = [result.poiList firstObject];
            self.baiduAddress = [NSString stringWithFormat:@"在%@附近",info.name];
        }else{
            self.baiduAddress = [NSString stringWithFormat:@"在%@附近",result.address];
        }
        [self.tableView reloadData];
    }
    
}
@end

